/**
 * 自定义jquery函数
 */
document.onkeydown = function (event) {
    var target, code, tag;
    if (!event) {
        event = window.event; //针对ie浏览器
        target = event.srcElement;
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "TEXTAREA") {
                return true;
            }
            else {
                return false;
            }
        }
    }
    else {
        target = event.target; //针对遵循w3c标准的浏览器，如Firefox
        code = event.keyCode;
        if (code == 13) {
            tag = target.tagName;
            if (tag == "INPUT") {
                return false;
            }
            else {
                return true;
            }
        }
    }
};
(function ($) {
    // 从json对象中获取值填入form表单中
    $.fn.jsonData = function (data) {
        var $name = $(this).find("[name]");
        $name.each(function () {
            var name = $(this).attr("name");
            $(this).val(data[name]);
        });
    };
    // 表单验证提交 配合validation.js使用
    $.fn.validFrom = function (callback) {
        $(this).Validform({
            tiptype: 2,
            ajaxPost: true,
            postonce: true,
            callback: function (data) {
                $.Hidemsg();
                callback(data);
            }
        });
    }
})(jQuery);

/** 全屏弹出 */
function full_add(title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

//时间格式化
function dataFormate(fmt, date) { //author: meizz
    var d = Number(date);
    if (d) {
        d = new Date(date);
    } else {
        return '';
    }
    var o = {
        "M+": d.getMonth() + 1, //月份 
        "d+": d.getDate(), //日 
        "h+": d.getHours(), //小时 
        "m+": d.getMinutes(), //分 
        "s+": d.getSeconds(), //秒 
        "q+": Math.floor((d.getMonth() + 3) / 3), //季度 
        "S": d.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

/**
 * 返回按钮
 */
$(document).on("click", ".btn-default", function () {
    layer_close();
})

/* 弹出窗口 */
function window_add(title, url, w, h) {
    layer_show(title, url, w, h);
}

/**
 *
 * @param formId 查询条件表单id
 * @param tmplId 模板id
 * @param showDivId 数据需要显示到的元素的id
 * @param pageDivId 分页dom id
 */
function initPage2(formId, tmplId, showDivId, pageDivId) {
    var $form = $("#" + formId);
    var $showDiv = $("#" + showDivId);
    var $pageDiv = $("#" + pageDivId);
    var url = $form.attr("action");
    var tmpl = $("#" + tmplId).html();
    $.ajax({
        dataType: "json",
        url: url,
        type: "post",
        async: false,
        data: $form.serialize(),
        success: function (data) {
            data = data.data;
            var totalPage = data.pagination.totalPage;
            var total = data.pagination.total;
            laytpl(tmpl).render(data, function (html) {
                $showDiv.html(html);
                var td = $(".text-m").find("td");
                for (var i = 0; i < td.length; i++) {
                    var ts = td[i].innerHTML;
                    if (ts == "null") {
                        td[i].innerHTML = "";
                    } else if (ts == "null(null)") {
                        td[i].innerHTML = "";
                    } else if (ts.indexOf("null") > 0) {
                        td[i].innerHTML = td[i].innerHTML.substring(0, ts.indexOf("null") - 1);
                    } else if (ts.indexOf("null") == 0) {
                        td[i].innerHTML = td[i].innerHTML.substring(ts.indexOf("null"), ts.indexOf("("));
                    }
                }

                $(".text-m").find("a").each(function () {
                    var title = $(this).attr("title");
                    if (title == "true") {
                        $(this).css("color", "#ccc");
                        $(this).attr("onclick", "");
                        $(this).attr("href", "javascript:;");
                    }
                });
                var data = [];
                var newdata = [];
                var cdata = "";
                $("#copy_all").click(function () {
                    newdata = [];
                    $("#view_coppy").find(":checkbox:checked").each(function () {
                        newdata.push($(this).val());
                        $('#fe_text').val(newdata);
                    })
                    data = [];
                })


                $("#view_coppy").find('input[type="checkbox"]').click(function () {
                    cdata = "";
                    $("#view_coppy").find(":checkbox:checked").each(function () {
                        data = [];
                        cdata = cdata + $(this).val() + ",";

                    });
                    cdata = cdata.substring(0, cdata.length - 1);

                    if ($(this).prop("checked")) {
                        if (cdata != "") {
                            data.push(cdata + "");
                        } else {
                            var broker_id = $(this).val();
                            data.push(broker_id + "");
                        }
                    } else {
                        data = [];
                        if (cdata != "") {
                            data.push(cdata + "");
                        }
                    }
                    if (newdata != null) {
                        data.push(newdata);
                    }
                    if (data.length > 0) {
                        newdata = [];
                        $('#fe_text').val(data);

                    }

                    if (newdata.length > 0) {
                        $('#fe_text').val(newdata);
                    }


                })

            });
            data = null;
            if (totalPage > 1) {//大于1页才显示分页
                laypage({
                    cont: $pageDiv, // 容器。值支持id名、原生dom对象，jquery对象。
                    pages: totalPage, // 通过后台拿到的总页数
                    curr: 1,
                    jump: function (e, first) { // 触发分页后的回调
                        $form.find("input[name='page']").val(e.curr);
                        if (!first) {
                            $.ajax({
                                dataType: "json",
                                url: url,
                                type: "post",
                                data: $form.serialize(),
                                success: function (res) {
                                    res = res.data;
                                    e.total = res.pagination.total;
                                    e.pages = e.last = res.pagination.totalPage; // 重新获取总页数，一般不用写
                                    laytpl(tmpl).render(res, function (html) {
                                        $showDiv.html(html);
                                        $(".laypage_main").append('<span>共' + total + "条记录,每页30条</span>");
                                        var td = $(".text-m").find("td");
                                        for (var i = 0; i < td.length; i++) {
                                            var ts = td[i].innerHTML;
                                            if (ts == "null") {
                                                td[i].innerHTML = "";
                                            } else if (ts == "null(null)") {
                                                td[i].innerHTML = "";
                                            } else if (ts.indexOf("null") > 0) {
                                                td[i].innerHTML = td[i].innerHTML.substring(0, ts.indexOf("null") - 1);
                                            } else if (ts.indexOf("null") == 0) {
                                                td[i].innerHTML = td[i].innerHTML.substring(ts.indexOf("null"), ts.indexOf("("));
                                            }
                                        }
                                        $(".text-m").find("a").each(function () {
                                            var title = $(this).attr("title");
                                            if (title == "true") {
                                                $(this).css("color", "#ccc")
                                                $(this).attr("onclick", "");
                                                $(this).attr("href", "javascript:;");
                                            }
                                        })
                                        var data = [];
                                        var newdata = [];
                                        var cdata = "";
                                        $("#copy_all").click(function () {
                                            newdata = [];
                                            $("#view_coppy").find(":checkbox:checked").each(function () {
                                                newdata.push($(this).val());
                                                $('#fe_text').val(newdata);
                                            })
                                            data = [];
                                        })


                                        $("#view_coppy").find('input[type="checkbox"]').click(function () {
                                            cdata = "";
                                            $("#view_coppy").find(":checkbox:checked").each(function () {
                                                data = [];
                                                cdata = cdata + $(this).val() + ",";

                                            })
                                            cdata = cdata.substring(0, cdata.length - 1);

                                            if ($(this).prop("checked")) {
                                                if (cdata != "") {
                                                    data.push(cdata + "");
                                                } else {
                                                    var broker_id = $(this).val();
                                                    data.push(broker_id + "");
                                                }
                                            } else {
                                                data = [];
                                                if (cdata != "") {
                                                    data.push(cdata + "");
                                                }
                                            }
                                            if (newdata != null) {
                                                data.push(newdata);
                                            }
                                            if (data.length > 0) {
                                                newdata = [];
                                                $('#fe_text').val(data);

                                            }

                                            if (newdata.length > 0) {
                                                $('#fe_text').val(newdata);
                                            }

                                        })

                                    });
                                }
                            });

                        } else {
                            $(".laypage_main").append('<span>共' + total + "条记录,每页30条</span>");
                        }
                    }
                });
            } else {
                $pageDiv.html('共1页    ' + total + "条记录");
            }

        }


    });

}

function initPage(formId, tmplId, showDivId, pageDivId, callback) {
    var $form = $("#" + formId);
    var $showDiv = $("#" + showDivId);
    var $pageDiv = $("#" + pageDivId);
    var url = $form.attr("action");
    var tmpl = $("#" + tmplId).html();
    $.ajax({
        dataType: "json",
        url: url,
        type: "post",
        data: $form.serialize(),
        success: function (data) {
            data = data.data;
            var totalPage = data.pagination.totalPage;
            var total = data.pagination.total;
            laytpl(tmpl).render(data, function (html) {
                $showDiv.html(html);
                var td = $(".text-m").find("td");
                for (var i = 0; i < td.length; i++) {
                    var ts = td[i].innerHTML;
                    if (ts == "null") {
                        td[i].innerHTML = "";
                    } else if (ts == "null(null)") {
                        td[i].innerHTML = "";
                    } else if (ts.indexOf("null") > 0) {
                        td[i].innerHTML = td[i].innerHTML.substring(0, ts.indexOf("null") - 1);
                    } else if (ts.indexOf("null") == 0) {
                        td[i].innerHTML = td[i].innerHTML.substring(ts.indexOf("null"), ts.indexOf("("));
                    }
                }

                $(".text-m").find("a").each(function () {
                    var title = $(this).attr("title");
                    if (title == "true") {
                        $(this).css("color", "#ccc")
                        $(this).attr("onclick", "");
                        $(this).attr("href", "javascript:;");
                    }
                })
                var data = [];
                var newdata = [];
                var cdata = "";
                $("#copy_all").click(function () {
                    newdata = [];
                    $("#view_coppy").find(":checkbox:checked").each(function () {
                        newdata.push($(this).val());
                        $('#fe_text').val(newdata);
                    });
                    data = [];
                });


                $("#view_coppy").find('input[type="checkbox"]').click(function () {
                    cdata = "";
                    $("#view_coppy").find(":checkbox:checked").each(function () {
                        data = [];
                        cdata = cdata + $(this).val() + ",";

                    });
                    cdata = cdata.substring(0, cdata.length - 1);

                    if ($(this).prop("checked")) {
                        if (cdata != "") {
                            data.push(cdata + "");
                        } else {
                            var broker_id = $(this).val();
                            data.push(broker_id + "");
                        }
                    } else {
                        data = [];
                        if (cdata != "") {
                            data.push(cdata + "");
                        }
                    }
                    if (newdata != null) {
                        data.push(newdata);
                    }
                    if (data.length > 0) {
                        newdata = [];
                        $('#fe_text').val(data);

                    }

                    if (newdata.length > 0) {
                        $('#fe_text').val(newdata);
                    }


                })

            });
            data = null;
            if (totalPage > 1) {//大于1页才显示分页
                laypage({
                    cont: $pageDiv, // 容器。值支持id名、原生dom对象，jquery对象。
                    pages: totalPage, // 通过后台拿到的总页数
                    curr: 1,
                    jump: function (e, first) { // 触发分页后的回调
                        $form.find("input[name='page']").val(e.curr);
                        if (!first) {
                            $.ajax({
                                dataType: "json",
                                url: url,
                                type: "post",
                                data: $form.serialize(),
                                success: function (res) {
                                    res = res.data;
                                    e.total = res.pagination.total;
                                    e.pages = e.last = res.pagination.totalPage; // 重新获取总页数，一般不用写
                                    laytpl(tmpl).render(res, function (html) {
                                        $showDiv.html(html);
                                        $(".laypage_main").append('<span>共' + total + "条记录,每页30条</span>");
                                        if ($("#copy_all").prop("checked")) {
                                            $("#copy_all").attr("checked", false);

                                        }
                                        var td = $(".text-m").find("td");
                                        for (var i = 0; i < td.length; i++) {
                                            var ts = td[i].innerHTML;
                                            if (ts == "null") {
                                                td[i].innerHTML = "";
                                            } else if (ts == "null(null)") {
                                                td[i].innerHTML = "";
                                            } else if (ts.indexOf("null") > 0) {
                                                td[i].innerHTML = td[i].innerHTML.substring(0, ts.indexOf("null") - 1);
                                            } else if (ts.indexOf("null") == 0) {
                                                td[i].innerHTML = td[i].innerHTML.substring(ts.indexOf("null"), ts.indexOf("("));
                                            }
                                        }
                                        $(".text-m").find("a").each(function () {
                                            var title = $(this).attr("title");
                                            if (title == "true") {
                                                $(this).css("color", "#ccc")
                                                $(this).attr("onclick", "");
                                                $(this).attr("href", "javascript:;");
                                            }
                                        });
                                        var data = [];
                                        var newdata = [];
                                        var cdata = "";
                                        $("#copy_all").click(function () {
                                            newdata = [];
                                            $("#view_coppy").find(":checkbox:checked").each(function () {
                                                newdata.push($(this).val());
                                                $('#fe_text').val(newdata);
                                            });
                                            data = [];
                                        });
                                        $("#view_coppy").find('input[type="checkbox"]').click(function () {
                                            cdata = "";
                                            $("#view_coppy").find(":checkbox:checked").each(function () {
                                                data = [];
                                                cdata = cdata + $(this).val() + ",";

                                            });
                                            cdata = cdata.substring(0, cdata.length - 1);

                                            if ($(this).prop("checked")) {
                                                if (cdata != "") {
                                                    data.push(cdata + "");
                                                } else {
                                                    var broker_id = $(this).val();
                                                    data.push(broker_id + "");
                                                }
                                            } else {
                                                data = [];
                                                if (cdata != "") {
                                                    data.push(cdata + "");
                                                }
                                            }
                                            if (newdata != null) {
                                                data.push(newdata);
                                            }
                                            if (data.length > 0) {
                                                newdata = [];
                                                $('#fe_text').val(data);

                                            }

                                            if (newdata.length > 0) {
                                                $('#fe_text').val(newdata);
                                            }
                                        })

                                    });
                                }
                            });

                        } else {
                            $(".laypage_main").append('<span>共' + total + "条记录,每页30条</span>");
                        }
                    }
                });
            } else {
                $pageDiv.html('共1页    ' + total + "条记录");
            }
            callback && callback();
        }


    });

}

function getRootPath() {
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPaht + projectName);
}

function initPageNoPage(formId, tmplId, showDivId) {
    var $form = $("#" + formId);
    var $showDiv = $("#" + showDivId);
    var url = $form.attr("action");
    var tmpl = $("#" + tmplId).html();
    $.ajax({
        dataType: "json",
        url: url,
        type: "post",
        data: $form.serialize(),
        success: function (data) {
            laytpl(tmpl).render(data, function (html) {
                $showDiv.html(html);
                var td = $(".text-m").find("td");
                for (var i = 0; i < td.length; i++) {
                    var ts = td[i].innerHTML;
                    if (ts == "null") {
                        td[i].innerHTML = "";
                    } else if (ts == "null(null)") {
                        td[i].innerHTML = "";
                    }
                }

            });
        }
    });
}

function removeByValue(arr, val) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == val) {
            arr.splice(i, 1);
            break;
        }
    }
}


function checkTime(startTimeID, endTimeID) {
    var $startTimeDiv = $("#" + startTimeID);
    var startTime = $startTimeDiv.val();
    var $endTimeDiv = $("#" + endTimeID);
    var endTime = $endTimeDiv.val();
    if (startTime == "" && endTime != "") {
        layer.msg("开始时间或结束时间不能为空", {icon: 5, time: 1000});
        return false;
    }
    if (startTime != "" && endTime == "") {
        layer.msg("开始时间或结束时间不能为空", {icon: 5, time: 1000});
        return false;
    }
    if (startTime > endTime) {
        layer.msg("开始时间不能大于结束时间", {icon: 5, time: 1000});
        return false;
    }
    return true;
}

function checkTimeStr(startTimeID, endTimeID, str) {
    var $startTimeDiv = $("#" + startTimeID);
    var startTime = $startTimeDiv.val();
    var $endTimeDiv = $("#" + endTimeID);
    var endTime = $endTimeDiv.val();

    if (startTime == "" && endTime == "") {
        layer.msg(str + "不能为空", {icon: 5, time: 1000});
        return false;
    }
    if (startTime == "" && endTime != "") {
        layer.msg(str + "开始时间或结束时间不能为空", {icon: 5, time: 1000});
        return false;
    }
    if (startTime != "" && endTime == "") {
        layer.msg(str + "开始时间或结束时间不能为空", {icon: 5, time: 1000});
        return false;
    }
    if (startTime > endTime) {
        layer.msg(str + "开始时间不能大于结束时间", {icon: 5, time: 1000});
        return false;
    }
    return true;
}

